package com.tanhua.server.controller;


import com.tanhua.model.vo.MovementVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.MovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 圈子控制层
 */

@Slf4j
@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;


    /**
     * 动态-发布
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity addMovement(MovementVo movementVo, MultipartFile[] imageContent) {
        movementService.addMovement(movementVo, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 我的动态
     */

    @RequestMapping(value = ("/all"), method = RequestMethod.GET)
    public ResponseEntity findPageMovement(@RequestParam(value = "page", defaultValue = "1") long page,
                                           @RequestParam(value = "pagesize", defaultValue = "10") long pagesize,
                                           Long userId) {
        PageResult<MovementsVo> pageResult = movementService.findPageMovement(page, pagesize, userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 好友动态
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity findPageMovementByFriend(@RequestParam(value = "page", defaultValue = "1") long page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") long pagesize) {
        PageResult<MovementsVo> pageResult = movementService.findPageMovementByFriend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @RequestMapping(value = ("/recommend"), method = RequestMethod.GET)
    public ResponseEntity findPageMovementByRecommend(@RequestParam(value = "page", defaultValue = "1") long page,
                                                      @RequestParam(value = "pagesize", defaultValue = "10") long pagesize) {
        PageResult<MovementsVo> pageResult = movementService.findPageMovementByRecommend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }



    /**
     * 单条动态
     * publishId:动态表主键id
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity findMovement(@PathVariable("id") String publishId){
        MovementsVo movementsVo = movementService.findMovement(publishId);
        log.debug("************单条动态*************"+movementsVo);
        return ResponseEntity.ok(movementsVo);
    }
    /**
     * 动态点赞
     */
    @RequestMapping(value = "/{id}/like", method = RequestMethod.GET)
    public ResponseEntity like(@PathVariable("id") String publishId) {
        Integer count = movementService.like(publishId);
        log.debug("************动态点赞{}*************", count);
        return ResponseEntity.ok(count);
    }


    /**
     * 动态取消点赞
     */
    @RequestMapping(value = "/{id}/dislike",method = RequestMethod.GET)
    public ResponseEntity dislike(@PathVariable("id") String publishId){
        Integer count = movementService.dislike(publishId);
        log.debug("************动态取消点赞{}*************",count);
        return ResponseEntity.ok(count);
}
}